package com.mhc.suzuki.controller;

import com.mhc.haval.api.dto.StaffDTO;
import com.mhc.haval.api.service.IStaffInfoService;
import com.mhc.haval.bo.EditPasswordBO;
import com.mhc.haval.exception.HavalCommonException;
import com.mhc.haval.security.model.User;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.form.ModifyPswForm;
import com.subaru.common.entity.BizResult;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

/**
 * 用户功能
 * Created by HMJ on 17/10/12 13:38.
 */
@Slf4j
@RestController
@RequestMapping(value = "/users")
public class UserController {

    @Autowired
    private IStaffInfoService iStaffInfoService;

    /**
     * 获取员工详细信息
     * @param logedUser
     * @return
     */
    @ApiOperation(value = "获取员工详细信息", notes = "根据url的id来获取员工详细信息")
    @RequestMapping(value = "/look", method = RequestMethod.GET)
    public BizResult getStaffInfo(@AuthenticationPrincipal User logedUser) {
        try {
            StaffDTO staffDTO = iStaffInfoService.queryStaffInfoByStaffId(logedUser.getStaffId());
            log.info("获取员工详细信息成功...");
            return BizResult.create(staffDTO, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        } catch (HavalCommonException e) {
            log.error("getStaffInfo HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }

    /**
     * 修改密码
     * @param modifyPswForm
     * @return
     */
    @ApiOperation(value = "修改密码", notes = "")
    @ApiImplicitParam(name = "modifyPswForm", value = "修改密码实体", required = true, paramType = "body", dataType = "ModifyPswForm")
    @RequestMapping(value = "/editPassword", method = RequestMethod.POST)
    public BizResult editPassword(@RequestBody ModifyPswForm modifyPswForm,@AuthenticationPrincipal User logedUser) {
        try {
            EditPasswordBO editPasswordBO = new EditPasswordBO();
            BeanCopierUtil.copy(modifyPswForm, editPasswordBO);
            editPasswordBO.setStaffId(logedUser.getStaffId());
            Boolean editFlag = iStaffInfoService.updateByPassword(editPasswordBO);
            log.info("修改密码成功...");
            return BizResult.create(editFlag, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());
        } catch (HavalCommonException e) {
            log.error("editPassword HavalCommonException",e);
            return BizResult.create(e.getCode(),e.getMessage());
        }
    }

}